package com.peng.androidlib.appbase;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.peng.androidlib.R;

/**
 * Created by Mr.Q on 20168/9/20.
 * 描述：
 *      自定义 Popupwindow 基类
 */

public abstract class BasePopupWindow extends PopupWindow implements PopupWindow.OnDismissListener {

    protected BaseActivity mContext;
    private View mAnchor;
    private int mAnimationStyle;
    private View mRoot;


    public BasePopupWindow(BaseActivity context) {
        this(context, null);
    }

    public BasePopupWindow(BaseActivity context, View anchor) {
        this(context, null, 0);
    }

    public BasePopupWindow(BaseActivity context, int animationStyle) {
        this(context, null, animationStyle);
    }

    public BasePopupWindow(BaseActivity context, View anchor, int animationStyle) {
        super(context);
        mContext = context;
        mAnchor = anchor;
        mAnimationStyle = animationStyle;
        init();

    }

    /**
     * 指定布局文件
     *
     * @return
     */
    public abstract int getLayoutResId();


    public View getRoot() {
        return mRoot;
    }

    private void init() {
        mRoot = LayoutInflater.from(mContext).inflate(getLayoutResId(), null);
        initPopupWindow(mRoot);
        initView(mRoot);

    }

    protected void initView(View root) {
    }

    private void initPopupWindow(View view) {
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        if (mAnimationStyle == 0) {//默认为淡入淡出效果
            mAnimationStyle = R.style.common_popwin_anim_fade_style;
        }
        setAnimationStyle(mAnimationStyle);
        setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable();
        setBackgroundDrawable(dw);
        // 如果该PopupWindow是有焦点的，那么在它弹出来之后，所有的按键和触摸事件都会被它截获，包括返回键。
        //PopupWindow响应返回键，必须给PopupWindow设置一个背景才行
        setFocusable(true);


        //设置为true  扫描枪失去作用
        setTouchable(true);
        setOnDismissListener(this);

    }

    public void close() {
        dismiss();
    }

    public void closeDelay() {
//        mContext.mRxManager.add(Observable.just("delay").delay(300, TimeUnit.MILLISECONDS)
//
//                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
//                    @Override
//                    public void call(Object o) {
//                        close();
//
//                    }
//                }));

    }

    public void changStatus() {
        if (isShowing()) {
            close();
        } else {
            show();
        }
    }


    public void show() {
        showAsDropDown(mAnchor);
    }


    //-------------popWindow显示的位置------------------------

    public void showInParentTop() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.TOP, 0, 0);
    }

    public void showInParentBottom() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    public void showInParentRight() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.RIGHT | Gravity.TOP, 0, 0);
    }

    public void showInParentLift() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.LEFT | Gravity.TOP, 0, 0);
    }

    public void showInParentCenter() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    //-------------popWindow显示的位置------------------------


    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        backgroundAlpha(mContext, 0.5f);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        backgroundAlpha(mContext, 0.5f);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        backgroundAlpha(mContext, 0.5f);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        backgroundAlpha(mContext, 0.5f);
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(mContext, 1.0f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {

        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
}
